package com.lz.football.module.activity.vip
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.activity.vip.VipVo;
	import com.lz.football.module.activity.IActivity;
	
	import common.BaseView.BaseView;
	import common.BaseView.GoodItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.ScrollPolicy;
	import lz.controls.containers.ScrollPane;
	import lz.controls.nickScroll.ScrollBarByMc;
	
	public class VipView extends BaseView implements IActivity
	{
		/**
		 *vip等级 
		 */
		private var vipLv:MovieClip;
		/**
		 *充值按钮 
		 */
		private var topupBtn:SimpleButton;
//		/**
//		 *领取按钮 
//		 */
//		private var getBtn:SimpleButton;
		/**
		 *下一等级效果 
		 */
		private var next:TextField;
		/**
		 *下一等级充值进度 
		 */
		private var pro:MovieClip;
		/**
		 * 
		 */
		private var _vipList:Array;
		
//		/**
//		 *滚动条 
//		 */
//		private var _scrollBar:ScrollBarByMc;
		/**
		 *滚动条容器 
		 */
		private var _scrollBarTarget:Sprite;
		/**
		 * 
		 */
		private var _dailyGoodItemVec:Vector.<GoodItem>;
		
		
		/**
		 *滚动条 
		 */
		private var _scroll:ScrollPane;
		
		public function VipView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.res.activity.vipActivity");
			if(symbolSkin == null)return;
			this.addChild(symbolSkin)
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			_windowBg = symbolSkin.vipBg;
			isCloseBtn = true;
			isDrag = true;
			onResizeHandler();
			moveClose(_viewWidth - 30,23);
			
			vipLv = symbolSkin.info.vipLv;
			topupBtn = symbolSkin.info.topupBtn;
//			getBtn = symbolSkin.info.getBtn;
			next = symbolSkin.info.next;
			pro = symbolSkin.info.pro;
			pro.gotoAndStop(40);
			vipLv.gotoAndStop(13);
			_vipList = AppData.getInstance().activityDatemanager.getVipList();
			initScroll();
			initItem();
			dailyAwardItem();
			setBar();
			addEvtListener();
		}
		override public function addEvtListener():void
		{
//			getBtn.addEventListener(MouseEvent.CLICK,onGetHandler);
			topupBtn.addEventListener(MouseEvent.CLICK,onTopupHandler);
		}
		
		protected function onTopupHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			JsClient.recharge();
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
//			getBtn.removeEventListener(MouseEvent.CLICK,onGetHandler);
			topupBtn.removeEventListener(MouseEvent.CLICK,onTopupHandler);
		}
		protected function onGetHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().clubDatamanager.clubInfo.VIP <=0)
			{
				Alert.showErrorTip("不是vip，不可以领取");
				return;
			}
//			getBtn.removeEventListener(MouseEvent.CLICK,onGetHandler);
			this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_VIP_DAILY_AWARD,{}));
		}
		/**
		 *初始化流动条 
		 * 
		 */
		private function initScroll():void
		{
			
			
			
			_scrollBarTarget = new Sprite();
//			_scrollBar = new ScrollBarByMc();
//			_scrollBar.setSize(649,268);
//			_scrollBar.target = _scrollBarTarget;
//			_scrollBar.upData();
//			_scrollBar.x = 24;
//			_scrollBar.y = 230;
//			this.addChild(_scrollBar);
			
			
			_scroll = new ScrollPane();
			_scroll.horizontalScrollPolicy = ScrollPolicy.OFF;
			_scroll.setSize(655,280);
			this.addChild(_scroll);
			_scroll.x = 30;
			_scroll.y = 215;
			_scroll.update();
			
		}
		/**
		 *初始化列表
		 * 
		 */
		private function initItem():void
		{
			var item:VipItem;
			var len:int =_vipList.length;
			for(var i:int = 0;i < len;i++)
			{
				item = new VipItem();
				item.initUI();
				item.setDate(_vipList[i]);
				_scrollBarTarget.addChild(item);
				item.y = (item.height + 3) * i;
				
//				_scroll.source = item;
				
			}
			_scroll.source = _scrollBarTarget;
			_scroll.update();
		}
		/**
		 *每日vip奖励数据 
		 * 
		 */
		private function dailyAwardDate($lv:int):VipVo
		{
			var len:int =_vipList.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_vipList[i].lv == $lv)
					return _vipList[i];
			}
			return null;
		}
		/**
		 *每日奖励 
		 * 
		 */
		private function dailyAwardItem():void
		{
			var vo:VipVo;
			var vip:int = AppData.getInstance().clubDatamanager.clubInfo.VIP;
//			vip = 12;
			if( vip<= 0)
			{
				vo =dailyAwardDate(1);
				vipLv.gotoAndStop(13);
			}else
			{
				vipLv.gotoAndStop(vip);
				vo =dailyAwardDate(vip );
			}
			
			
			var arr:Array = vo.daily;
			
			var len:int = arr.length;
			var i:int;
			var item:GoodItem;
			_dailyGoodItemVec = new Vector.<GoodItem>();
			for(i = 0;i < len;i++)
			{
				item = new GoodItem();
				item.initUI();
				item.data = arr[i].goods;
				item.showNum(arr[i].num);
				this.addChild(item);
				if(len == 1)
				{
					item.x = 496 + (item.width + 1)* i;
				}else if(len == 2)
				{
					item.x = 462 + (item.width + 1)* i;
				}else if(len == 3)
				{
					item.x = 430 + (item.width + 1)* i;
				}else
				{
					item.x = 410 + (item.width + 1)* i;
				}
				
				_dailyGoodItemVec.push(item);
				item.y = 112;
			}
		}
		/**
		 *设置vip进度 
		 * 
		 */
		private function setBar():void
		{
			var vo:VipVo;
			var vip:int = AppData.getInstance().clubDatamanager.clubInfo.VIP;
			//当前vip等级 
			var currentVo:VipVo = dailyAwardDate(vip);
			//下一vip等级
			var nextVo:VipVo = dailyAwardDate(vip+1);
			if(nextVo == null)//达到最高级
			{
				symbolSkin.info.next.text = "";
			}else
			{
				var c:int = nextVo.cost - AppData.getInstance().clubDatamanager.clubInfo.cashpoint_vip //- (currentVo== null?0:currentVo.cost);
				var n:int = nextVo.cost - (currentVo == null?0:currentVo.cost);
				pro.gotoAndStop(Math.ceil(((n -c) / n) * 100));
				
				symbolSkin.info.next.text = "下一级VIP"+(vip+ 1)+"只需充值"+
					(nextVo.cost - AppData.getInstance().clubDatamanager.clubInfo.cashpoint_vip)+
					"点券便可达到！"
			}
//			vipLv.gotoAndStop(vip);
			if(AppData.getInstance().clubDatamanager.clubInfo.vip_reward_every ==1)
			{
//				getBtn.filters = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0,
//					0.3086, 0.6094, 0.082, 0, 0,
//					0.3086, 0.6094, 0.082, 0, 0,
//					0,0,0,1,0,])];
//				getBtn.mouseEnabled = false;
			}
		}
		/**
		 *基本信息 
		 * @param $obj
		 * 
		 */		
		public function baseinformation($obj:Object):void
		{
		}
		/**
		 *领取奖励返回 
		 * @param $obj
		 * 
		 */		
		public function award($obj:Object):void
		{
			addEvtListener();
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new ActivityEvent(ModuleEvent.REMOVE_MODULE));
		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(_dailyGoodItemVec.length)
			{
				_dailyGoodItemVec.pop().destroyView();
			}
			var vipItem:VipItem;
			while(_scrollBarTarget.numChildren)
			{
				vipItem = _scrollBarTarget.removeChildAt(0) as VipItem;
				vipItem.destroyView();
				vipItem = null;
			}
//			_scrollBar.destroyView();
//			_scrollBar = null;
			_scroll.destroy();
			_scroll = null;
			_dailyGoodItemVec = null;
			super.destroyView();
		}
			
	}
}